package com.augurit.wf.common.tag.opinion;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.components.Component;
import org.apache.struts2.views.jsp.ComponentTagSupport;

import com.augurit.common.util.lang.DateUtils;
import com.augurit.wf.common.form.TraceRecord;
import com.opensymphony.xwork2.util.ValueStack;

public class WfOpinionTag extends ComponentTagSupport {
	private static final long serialVersionUID = 77666L;
	private static final String ACTIVITY_SPEPARTOR = ",";
	public static final String WF_OPINIONS = "wfOpinions";
	
	private String name;
	private String activities;
	private String opinions;
	private String debug;

	@Override
	public Component getBean(ValueStack stack, HttpServletRequest req,
			HttpServletResponse res) {
		WfOpinion wfOpinion = new WfOpinion(stack);
		buildOpinions(req);
		return wfOpinion;
	}
	
	public void buildOpinions(ServletRequest req){
		
		@SuppressWarnings({"unchecked"})
		Map<String, List<TraceRecord>> map = (Map<String, List<TraceRecord>>)req.getAttribute(WF_OPINIONS);
		StringBuilder opinionsStr = new StringBuilder("");
		if(map != null){
			String[] activityNames = activities.split(ACTIVITY_SPEPARTOR);
			
			if(activityNames != null && activityNames.length > 0){
			
				for(int i = 0; i < activityNames.length; i++){
					List<TraceRecord> traceRecords = map.get(activityNames[i]);
					if(traceRecords != null && traceRecords.size() > 0){
						
						for(TraceRecord traceRecord:traceRecords){
							if(traceRecord.getHandleComments() != null && traceRecord.getHandleComments().trim().length() > 0){
								opinionsStr.append(traceRecord.getHandleComments());
								opinionsStr.append("<br>");
								opinionsStr.append("&nbsp;&nbsp;");
								opinionsStr.append(traceRecord.getAssignee());
								opinionsStr.append("&nbsp;&nbsp;");
								opinionsStr.append(DateUtils.dateTimeToString(traceRecord.getHandleCommentsTime(), DateUtils.YYYY_MM_DD_HH_MM_SS));
								opinionsStr.append("<br>");
							}
						}
					}
				}
			}
		}
		opinions = opinionsStr.toString();
	}
	
	//获得参数
	protected void populateParams(){
		super.populateParams();
		WfOpinion wfOpinion = (WfOpinion)component;
		wfOpinion.setOpinions(opinions);
	}

	public String getOpinions() {
		return opinions;
	}

	public void setOpinions(String opinions) {
		this.opinions = opinions;
	}

	public String getActivities() {
		return activities;
	}

	public void setActivities(String activities) {
		this.activities = activities;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public String getDebug() {
		return debug;
	}

	public void setDebug(String debug) {
		this.debug = debug;
	}
}
